﻿using System;
using System.Collections;
using System.Diagnostics;
using System.ServiceModel;
using Com.Binaria.Core.Web;
using Com.Binaria.Core.Presentacion;

namespace Com.Binaria.SISCOB.Web
{
    #region HELPERs UTILIZADOS POR EL PRESENTER
    public sealed class Services
    {
        #region IMPLEMENTACION DE UN SINGLETON
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static object syncRoot = new object();

        /// <summary>
        /// Allocate ourselves. We have a private constructor, so no one else can.
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private static volatile Services _instance = null; // new ApplicationSetting();

        /// <summary>
        /// Access SiteStructure.Instance to get the singleton object.
        /// Then call methods on that instance.
        /// </summary>
        public static Services Instance
        {
            get
            {
                // only create a new instance if one doesn't already exist.
                if (_instance == null)
                {
                    // use this lock to ensure that only one thread is access
                    // this block of code at once.
                    lock (syncRoot)
                    {
                        if (_instance == null)
                            _instance = new Services();
                    }
                }
                // return instance where it was just created or already existed.
                return _instance;
            }
        }
        #endregion

        #region ETIQUETAS MIEMBRO
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private Hashtable servicios_ = new Hashtable(16);
        #endregion

        #region METODOS A EXPONER AL USUARIO
        public TSvc GetSvc<TSvc>()
        {
            TSvc svc_ = default(TSvc);
            string name_ = typeof(TSvc).Name;
            //administrar lista personalizada
            if (!servicios_.ContainsKey(name_))
            {
                svc_ = (TSvc)ServiceLocator.getInstance().getObject(name_);
                if (servicios_.Count > 14)
                {
                    //Quitar el Primer Elemento del Diccionario
                }
                servicios_.Add(name_, svc_);
            }
            //
            return (TSvc)servicios_[name_];
        }

        public TSvc GetSvcClient<TSvc>()
        {
            return GetSvcClient<TSvc>(string.Format("NetTcpBinding_{0}", typeof(TSvc).Name));
        }

        public TSvc GetSvcClient<TSvc>(string EndPointName)
        {
            string name_ = typeof(TSvc).Name;
            ChannelFactory<TSvc> factory = null;
            //administrar lista personalizada
            if (!servicios_.ContainsKey(name_))
            {
                try
                {
                    factory = new ChannelFactory<TSvc>(EndPointName);
                    //
                    TSvc _oChannel = (TSvc)factory.CreateChannel();
                    //
                    servicios_.Add(name_, _oChannel);
                }
                catch { throw; }
            }
            //
            return (TSvc)servicios_[name_];
        }
        #endregion
    }

    public class RelacionItem : IRelacion
    {
        #region Miembros de IRelacion
        public IList DataSource
        {
            get;
            set;
        }

        public string IdentityName
        {
            get;
            set;
        }
        public string[] Target
        {
            get;
            set;
        }
        #endregion
    }
    #endregion
}